package com.kartoshka.painters 
{
	import com.kartoshka.interfaces.IPainter;

	import flash.display.Graphics;

	/**
	 * @author Alexander Zats, 2009
	 */
	public class CompositePainter implements IPainter 
	{

		protected var _cornerRadius : Number;
		protected var _painters : Array;

		public function CompositePainter( ...painters )
		{
			_painters = [];
			
			if ( painters ) addPainter( painters );
		}

		public function addPainter( ...painters ) : void
		{
			for ( var i : int = 0; i < painters.length ; i++ )
			{
				if ( painters[ i ] is Array )
				{
					for ( var j : int = 0; j < painters[ i ].length ; j++ )
					{
						if ( painters[ i ][ j ] is IPainter ) 
						{
							if ( ! isNaN( _cornerRadius ) ) ( painters[ i ] as IPainter ).cornerRadius = _cornerRadius;
							_painters[ _painters.length ] = painters[ i ][ j ];
						}
					}
				}
				else
				{
					if ( painters[ i ] is IPainter ) 
					{
						if ( ! isNaN( _cornerRadius ) ) ( painters[ i ] as IPainter ).cornerRadius = _cornerRadius;
						_painters[ _painters.length ] = painters[ i ];
					}
				}
			}
		}

		public function draw(graphics : Graphics, width : Number, height : Number) : void
		{
			for ( var i : int = 0; i < _painters.length ; i++ )
			{
				( _painters[ i ] as IPainter ).draw( graphics, width, height );
			}
		}
		
		/*
		 * If you will set <code>cornerRadius</code> it'll overwrite <code>cornerRadius</code> property for each painter added before or after
		 */
		public function set cornerRadius( value : Number ) : void
		{
			_cornerRadius = value;
			if ( isNaN( value ) ) return;
			for each ( var painter : IPainter in _painters )
			{
				painter.cornerRadius = _cornerRadius;
			}
		}
		
		public function get cornerRadius() : Number
		{
			return _cornerRadius;
		}
	}
}
